package cn.fangline.wo.service;

import cn.fangline.wo.mapper.HouseMapper;
import cn.fangline.wo.model.House;
import cn.fangline.wo.model.HouseImg;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 抽离 on 2017/5/3 0003.
 */
@Service
public class HouseService {

    @Autowired
    HouseMapper houseMapper;


    public Page<House> findHouses(Page<House> page, Integer corpId, Integer cityId, String section){
        House house = new House();
        Page<House> housePage = null;
        if(StringUtils.isBlank(section)){
            housePage = house.selectPage(page,new EntityWrapper<House>()
                    .eq("corp_id",corpId)
                    .eq("city_id",cityId)
//                    .notIn("net",new Integer[]{0})
                    .orderBy("(case when create_user is null then 0 else create_user end)", false)
                    .orderBy("(case when origin_count is null then 0 else origin_count end)", false)
                    .orderBy("create_time", false)
            );
//            housePage = house.selectPage(page, new EntityWrapper<House>().where("corp_id = {0} AND city_id = {1} AND net !={3}", corpId, cityId, 0).orderBy("create_time"));
        }else{
            housePage = house.selectPage(page,new EntityWrapper<House>()
                    .eq("corp_id",corpId)
                    .eq("city_id",cityId)
                    .like("section",section)
//                 .notIn("net",new Integer[]{0}
                    .orderBy("(case when create_user is null then 0 else create_user end)", false)
                    .orderBy("(case when origin_count is null then 0 else origin_count end)", false)
                    .orderBy("create_time", false)
            );
//            housePage = house.selectPage(page, new EntityWrapper<House>().where("corp_id = {0} AND city_id = {1} AND section={2} AND net !={3}", corpId, cityId, section,0).orderBy("create_time"));
        }

        return housePage;
    }

    public House getHouse(Long houseId){
        House house = new House();
        house = house.selectById(houseId);
        HouseImg houseImg = new HouseImg();
        List<HouseImg> houseImgs = houseImg.selectList(new EntityWrapper<HouseImg>().eq("house_id",houseId));
        house.setHouseImg(houseImgs);
        return house;
    }

    public List<House> searchHouse(String section, Integer cityId, Long corpId){
        List<House> houses = houseMapper.searchHouse(section, cityId, corpId);
        System.out.println("----------=" + houses);
        return houses;
    }
 }
